package com.jwgf.adapter;

import android.util.TypedValue;

import com.jwgf.common.AppContext;

/**
 * Created by Jacky on 2018/4/2.
 */

public class DensityUtils {

    private DensityUtils() {
    }

    /**
     * dp转px
     */
    public static int dp2px(float dpVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                dpVal, AppContext.getInstance().getResources().getDisplayMetrics());
    }

    /**
     * sp转px
     */
    public static int sp2px(float spVal) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
                spVal, AppContext.getInstance().getResources().getDisplayMetrics());
    }

    /**
     * px转dp
     */
    public static float px2dp(float pxVal) {
        final float scale = AppContext.getInstance().getResources().getDisplayMetrics().density;
        return (pxVal / scale);
    }

    /**
     * px转sp
     */
    public static float px2sp(float pxVal) {
        return (pxVal / AppContext.getInstance().getResources().getDisplayMetrics().scaledDensity);
    }
}
